package com.muqi.app.qlearn.student.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.adapter.CommonAdapter;
import com.muqi.app.qlearn.adapter.CommonViewHolder;
import com.muqi.app.qlearn.adapter.HomeWorkAdapter;
import com.muqi.app.qlearn.modles.Examination;
import com.muqi.app.qlearn.modles.HomeWorkBean;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.qlearn.student.R.id;
import com.muqi.app.qlearn.student.R.layout;
import com.muqi.app.qlearn.ui.MyHomeWorkActivity;
import com.muqi.app.widget.pulltorefresh2.XListView;
import com.muqi.app.widget.pulltorefresh2.XListView.IXListViewListener;
import com.muqi.app.widget.pulltorefresh2.XListView.OnXScrollListener;

import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.TextView;

public class WeakActivity extends BaseFragmentActivity implements
		IXListViewListener {

	private XListView listView;
	private TextView tvTitle;
	private TextView itemContent;
	private TextView itemDetial;
	private TextView itemCount;
	private TextView itemName;
	private int type;
	private String url;
	private List<Examination> listData = new ArrayList<Examination>();
	private int page = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_weak);
	}

	@Override
	protected void onInit() {
		type = getIntent().getIntExtra("type", 0);
		listView = (XListView) findViewById(R.id.weak_lv);
		tvTitle = (TextView) findViewById(R.id.weak_title);
		switch (type) {
		case 0:
			tvTitle.setText("知识弱项");
			url = "";
			break;
		case 1:
			tvTitle.setText("能力弱项");
			url = "";
			break;
		case 2:
			tvTitle.setText("技能弱项");
			url = "";
			break;
		default:
			break;
		}
		listView.setPullLoadEnable(false);
		listView.setPullRefreshEnable(true);
		listView.setXListViewListener(this);
		listView.setAdapter(adapter);
	}

	@Override
	protected void onData() {
		loadWeakData();

	}

	/**
	 * 加载试题数据
	 */
	private void loadWeakData() {
		if (NetWorkUtils.isNetworkAvailable(this)) {
			HashMap<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("token", mSpUtil.getToken());
			sendMap.put("student_id", mSpUtil.getCurrentChildId());
			sendMap.put("class_manager_id", mSpUtil.getChildClassId());
			sendMap.put("page", page);
			String requestUrl = ParamsUtils.buildParams(
					NetWorkApi.GET_EXAMINATION_API, sendMap);
			MyAsyncHttp.get(this, requestUrl, new AsyncCallBackListener() {

				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode,
						String request, String result) {
					onLoadView();
					hideLoading();
					if (resuleCode == RESULTCODE.SUCCESS) {
						List<Examination> getLists = ResponseUtils
								.getExaminationInfos(mContext, result);
						if (getLists != null && getLists.size() > 0) {
							if (page == 1) {

								listData.clear();

							}
							for (Examination examination : getLists) {
								listData.add(examination);
							}
							adapter.notifyDataSetChanged();

							if (getLists.size() < 10) {
								listView.setPullLoadEnable(false);
							} else {
								listView.setPullLoadEnable(true);
								page++;
							}
						}
					}
				}
			});
		} else {
			hideLoading();
			ShowToast.showShort(this, R.string.net_break);// 提示无网络
		}

	}

	private CommonAdapter<Examination> adapter = new CommonAdapter<Examination>(
			this, listData, R.layout.item_exmination) {

		@Override
		protected void convertView(View item, final Examination t) {
			if (t != null) {
				itemContent = CommonViewHolder.get(item, R.id.item_content);
				itemCount = CommonViewHolder.get(item, R.id.item_count);
				itemDetial = CommonViewHolder.get(item, R.id.item_detial);
				itemName=CommonViewHolder.get(item, R.id.item_name);
				itemContent.setText(t.q_name);
				itemCount.setText(t.comment_num + "人回答");
				itemName.setText(t.type);
				itemDetial.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Intent intent = new Intent(mContext,
								ResolveActivity.class);
						intent.putExtra("resolve", t);
						startActivity(intent);
					}
				});
			}

		}
	};

	public void onBack(View v) {
		finish();
	}

	@Override
	public void onRefresh() {
		page = 1;
		loadWeakData();

	}

	@Override
	public void onLoadMore() {
		loadWeakData();

	}

	private void onLoadView() {
		listView.stopRefresh();
		listView.stopLoadMore();
		listView.setRefreshTime("刚刚");
	}
}
